package cn.xfdzx.android.apps.shop.fragment.market;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.adapter.XFDProductAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.contract.ClassProductContract;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ClassProductPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.UtilsData;

/**
 * @author gzk
 * @date 2020/3/26 0026
 * description：产品
 */
public class ProductFragment extends BaseMvpFragment<ClassProductPresenter> implements ClassProductContract.View {


    RecyclerView rvProduct;
    SmartRefreshLayout mRefreshLayout;

    private XFDProductAdapter XFDProductAdapter;
    private int type = 1;//类别 0-零售 1-批发
    private int pages=1;
    private int limit = Constants.LIMIT;//每页多少条
    private String sid = "";
    private String keyWrod = "";

    private LinearLayout ll_hint;

    private List<HomeSearchXFDGoodBeen.DataBean> mList = new ArrayList<>();
private String user_curent_lng,user_curent_lat;
    @Override
    protected void initView(View view) {
          user_curent_lng = UtilsData.getPreference(getActivity(), "coordinates").getString("coordinates_lng");
          user_curent_lat = UtilsData.getPreference(getActivity(), "coordinates").getString("coordinates_lat");

        ll_hint=view.findViewById(R.id.ll_hint);
        rvProduct=view.findViewById(R.id.rv_product);
        mRefreshLayout=view.findViewById(R.id.refreshLayout);

        mPresenter = new ClassProductPresenter();
        mPresenter.attachView(this);
        sid= getArguments().getString("id");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvProduct.setLayoutManager(linearLayoutManager);
        XFDProductAdapter = new XFDProductAdapter(getContext(),mList);
        XFDProductAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(getContext(), XFDGoodsDeatailsActivity.class);
                intent.putExtra("good_id",mList.get(position).getId()+"");
                startActivity(intent);
            }
        });
        rvProduct.setAdapter(XFDProductAdapter);


        mPresenter.initData(true, true,user_curent_lng,user_curent_lat, sid, pages + "", limit + "", type + "", "0", "", "", "", "", "");

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                pages = 1;
                mPresenter.initData(true, true, user_curent_lng,user_curent_lat,sid, pages + "", limit + "", type + "", "0", "", "", "", "", "");

            }
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_product;
    }

    @Override
    public void showLoading() {
        ProgressDialog.getInstance().show(getActivity());
    }


    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onInitSuccess(HomeSearchXFDGoodBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (pages == 1) {
                mList.clear();
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.initData(false, false,user_curent_lng,user_curent_lat,sid, pages + "", limit + "", type + "", "0", "", "", "", "", "");
                    }
                });
            }
            if (bean.getData().size() == 0) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                pages++;
            }
            mList.addAll(bean.getData());
            if (mList.size()==0){
                ll_hint.setVisibility(View.VISIBLE);
            }else {
                ll_hint.setVisibility(View.GONE);
            }
            XFDProductAdapter.notifyDataSetChanged();
        }else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void finishRefresh() {
            mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
            mRefreshLayout.finishLoadMore(true);//加载完成
    }

    public static ProductFragment newInstance(String id) {
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
//        bundle.putLong(Constants.UID, userUid);
        ProductFragment fragment = new ProductFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroy() {
        mList.clear();
        mList=null;
        super.onDestroy();
    }
}
